clear all;clc
% load tamper_img
load stego_cover
load glass.mat
load key
load p 
load mark_block
%% 切换被篡改的图片
orgin_stego=stego_cover;%保存一下原始含秘图片
addpath(genpath('.'));  %添加工具箱路径
[width,height]=size(stego_cover);
[w_m,h_m]=size(mark_block);
detetion_img=zeros(width,height); %设置检测图形
%% embed_msg&bitstream生成
rng(key); %%key为伪随机序列种子
msg = num2str(uint8(rand(1,width*height)));
msg=strrep(msg,' ','');%剔除空格
embed_msg=msg(1:p-1);%获取嵌入的秘密信息
count=1;%元胞数组指针
%% 恶意修改
% tamper_m=imread('./tamper Sets/Peppers.tif');
% cover=imread('./Image Sets/Peppers.pgm');
% 彩图
load ./'Color Sets'/C1.mat
load ./'Color Sets'/Ct1.mat
tamper_m=Ct1;cover=C1;
% tamper_m=imread('D:\Program Files\Polyspace\R2020b\bin\Source_code_1\Color Sets\32t.tif');
% cover=imread('D:\Program Files\Polyspace\R2020b\bin\Source_code_1\Color Sets\32.tif');
% cover=double(imread('./JPEG Sets/lena.jpg'));
orgin_stego_cover=replace_img(double(stego_cover),double(cover(:,:,2)),double(tamper_m(:,:,2))); 
% for qualityFactor=10:10:100
s=1;%指向embed_msg的指针
bitstream='';%设置初始提取比特流
N=4;
%% 开始篡改，经历恶意修改。开始鲁棒实验
stego_cover=orgin_stego_cover;
%% 鲁棒性测试,经历善意修改
%加噪
for i=1:10
stego_cover=orgin_stego_cover;
% stego_cover=double(imnoise(uint8(stego_cover),'gaussian',0,i*10^-7));
%压缩 50 :5 :100
% imwrite(uint8(stego_cover),'jpegImageAttacked.jpg','jpg','quality',qualityFactor);
% stego_cover = double(imread('jpegImageAttacked.jpg'));
% Attack_image=Attack(double(stego_cover));
% stego_cover=round(Attack_image.noiseGauss(10^-5));% 10^-6 一个分水岭
% stego_cover=awgn(stego_cover,10);
%% 获取分块策略
for x=1:w_m
    for y=1:h_m
                    %% 1*1分块策略——提取16位
        if mark_block(x,y)==2
            bits=new_Extract_process_1(stego_cover((x-1)*N+1:x*N,(y-1)*N+1:y*N));
            if strcmp(bits,embed_msg(s:s+15))~=1 %篡改了
                detetion_img=detect_process_1(detetion_img,x,y,bits,embed_msg(s:s+15));
            end
            s=s+16;%应该是+16
        end
                     %% 2*2分块策略——提取8位
        if mark_block(x,y)==1
            bits=new_Extract_process_2(stego_cover((x-1)*N+1:x*N,(y-1)*N+1:y*N));
            if strcmp(bits,embed_msg(s:s+7))~=1
                detetion_img=detect_process_2(detetion_img,x,y,bits,embed_msg(s:s+7));
            end
            s=s+8;%应该是+8
        end
                    %% 4*4分块策略——提取9位
        if mark_block(x,y)==0
            delta=3;
            bits=Extract_process_3(stego_cover((x-1)*N+1:x*N,(y-1)*N+1:y*N),delta);
%             if strcmp(bits,embed_msg(s:s+8))~=1
            if mycomparebit(bits,embed_msg(s:s+8))
                detetion_img=detect_process_3(detetion_img,x,y,bits,embed_msg(s:s+8));
            end
            s=s+9;
        end
        bitstream=[bitstream bits];
    end
end
%     X=mychar2double(embed_msg);
%     Y=mychar2double(bitstream);
%     N=sum((X-Y)~=0);
%     P=N/length(embed_msg);%计算差异比例
%     N=4;%还原N
if sum(sum(detetion_img))==0
   disp('No tamper!!')
    %% 测测PSNR是多少
%     PSNR=find_psnr(cover,double(stego_cover))
else
%     subplot(221),
%     imshow('lena.pgm');
%     title('原始的图片')
%     subplot(222),
%     imshow(stego_cover,[]);
%     title('被篡改的图片')
%     subplot(223),
    %先腐蚀再膨胀
%      trans_detetion_img = bwmorph(detetion_img,'erode',1);  %膨胀
%      trans_detetion_img = bwmorph(trans_detetion_img,'dilate',1);  %侵蚀
     %先膨胀再腐蚀
     trans_detetion_img = bwmorph(detetion_img,'dilate',2);  %膨胀
     trans_detetion_img = bwmorph(trans_detetion_img,'erode',2);  %侵蚀
%      trans_detetion_img = bwmorph(double(detetion_img),'clean');  %闭运算
%      imshow(detetion_img,[]);
%     subplot(121)
%         imshow(trans_detetion_img,[]);
%      title('篡改检测与定位,形态学');
% %     subplot(224),
%     subplot(122)
%      imshow(detetion_img,[]);
%     title('篡改检测与定位，非形态学');
    Accruy_struct1=cacul_accury(orgin_stego,orgin_stego_cover,detetion_img);
    Accruy_struct2=cacul_accury(orgin_stego,orgin_stego_cover,trans_detetion_img);
    Rob_Acc{count,1}=Accruy_struct1;
    Rob_Acc{count,2}=Accruy_struct2;
    count=count+1;
    s=1;
    bitstream='';
end
end
save Rob_Acc Rob_Acc
% imshow(trans_detetion_img,[]);


